package com.fenghongzhang.module_main.glide.nice_pic

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 *@author Wcj
 *@description
 *@date 2022/7/5 13:12
 */
class SpacesItemDecoration(private val space: Int, private val column: Int = 3) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildAdapterPosition(view)
        // 每个span分配的间隔大小
        val spanSpace = space * (column + 1) / column
        // 列索引
        val colIndex = position % column
        //行索引
        val rowIndex = position / column
        // 列左右空隙
        outRect.left = space * (colIndex + 1) - spanSpace * colIndex
        outRect.right = spanSpace * (colIndex + 1) - space * (colIndex + 1)
        //行上下空隙
        outRect.top = space * (rowIndex + 1) - spanSpace * rowIndex
        outRect.bottom = spanSpace * (rowIndex + 1) - space * (rowIndex + 1)
        // 行间
    }
}
